package cn.Controller;

import cn.CommonUtils.ApplicationConfigurationSingletonFactory;
import cn.MetaData.Annotation.ToDo;
import cn.Service.ExampleService;
import cn.Service.ServiceInteface;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class IndexController {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IndexController.class);

    //  返回视图
    @RequestMapping(value = "/"  , method = RequestMethod.GET)
    public ModelAndView proc1(){
        ModelAndView modelAndView = new ModelAndView("home");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/hello")
    public String proc2(HttpServletRequest request){
        log.info(request.getContextPath());
        log.info(request.getServerName()+request.getServerPort());
        log.info(request.getSession().getServletContext().getContextPath());

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServiceInteface serviceInteface =(ExampleService) applicationContext.getBean("aaa");
        System.out.println(serviceInteface.testProc());
        serviceInteface.testProc();
        return "good eveing";
    }

    @ResponseBody
    @RequestMapping(value = "/oneParam")
    public String oneParamProc( String smsVerifyCode ){
        JSONObject result = new JSONObject();
        result.put("status", "Param : " + smsVerifyCode);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/register")
    public String registerProc( String smsVerifyCode , HttpServletRequest request){
        Object smsVerifyCodeErrorInfoStr = request.getAttribute("smsVerifyCodeErrorInfo");
        String smsVerifyCodeErrorInfo = (String)smsVerifyCodeErrorInfoStr;
        JSONObject result = new JSONObject();
        result.put("state", "success");
        result.put("info", smsVerifyCodeErrorInfo);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/test/test1")
    @ToDo
    public String testTest1Proc(){

//        LogBack


        for (int i = 0; i < 50; i++) {
            if (i % 2 == 0) {
                log.warn("this is a warning message!");
                continue;
            }

            log.info("this is a info message");

        }

        return "success";

    }

    @ResponseBody
    @RequestMapping(value = "/test/test2")
    @ToDo
    public String testTest2Proc(String param1 , Integer param2){
        log.info("come in test test1 procduce");
        return "success";

    }

    private String resultStr = new String();

    /**
     * 获取配置集合
     * @return
     */
    @RequestMapping("/getconfig")
    @ResponseBody
    public String getConfigProc(){
        resultStr = "";
        Map<String,String> list = ApplicationConfigurationSingletonFactory.getInstance();
        list.forEach( (k,v)->{
            resultStr +=v;
        });

        return resultStr;
    }

    /**
     * 重载配置集合
     * @return
     */
    @RequestMapping("/reloadconfig")
    @ResponseBody
    public String getConfigProc2(){
        resultStr = "";
        Map<String,String> list = ApplicationConfigurationSingletonFactory.reloadApplicationConfiguration();
        list.forEach( (k,v)->{
            resultStr +=v;
        });

        return resultStr;
    }

    /**
     * 读取指定配置项 - restful api
     * @param key
     * @return
     */
    @RequestMapping("/getOneConfig/{key}")
    @ResponseBody
    public String getOneConfig(@PathVariable String key) {
        String value = ApplicationConfigurationSingletonFactory.getValueOfConfiguration(key);
        return value;
    }
}
